package com.ruoyi.develop.lvFapiao.service.impl;

import java.math.BigDecimal;
import java.util.List;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.DateUtils;
        import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import javax.annotation.Resource;

import com.ruoyi.constants.BusinessConstants;
import com.ruoyi.develop.goodsSalebuy.domain.GoodsSalebuy;
import com.ruoyi.develop.goodsSalebuy.mapper.GoodsSalebuyMapper;
import com.ruoyi.develop.goodsSalebuyall.domain.GoodsSalebuyall;
import com.ruoyi.develop.goodsSalebuyall.mapper.GoodsSalebuyallMapper;
import com.ruoyi.develop.lvBuy.domain.LvBuy;
import com.ruoyi.develop.lvBuy.mapper.LvBuyMapper;
import org.springframework.stereotype.Service;
import com.ruoyi.develop.lvFapiao.mapper.LvFapiaoMapper;
import com.ruoyi.develop.lvFapiao.domain.LvFapiao;
import com.ruoyi.develop.lvFapiao.service.ILvFapiaoService;

/**
 * 到货发票Service业务层处理
 * @author qh
 * @date 2024-09-14
 */
@Service
public class LvFapiaoServiceImpl implements ILvFapiaoService {
    @Resource
    private LvFapiaoMapper lvFapiaoMapper;
    @Resource
    private LvBuyMapper lvBuyMapper;
    @Resource
    private GoodsSalebuyallMapper goodsSalebuyallMapper;
    @Resource
    private GoodsSalebuyMapper goodsSalebuyMapper;

    /**
     * 查询到货发票
     * @param id 到货发票主键
     * @return 到货发票
     */
    @Override
    public LvFapiao selectById(String id) {
        return lvFapiaoMapper.selectById(id);
    }

    /**
     * 查询到货发票列表
     * @param entity 到货发票
     * @return 到货发票
     */
    @Override
    public List<LvFapiao> selectList(LvFapiao entity) {
        return lvFapiaoMapper.selectList(entity);
    }

    /**
     * 新增到货发票
     * @param entity 到货发票
     * @return 结果
     */
    @Override
    public int insert(LvFapiao entity) {
        if(StringUtils.isBlank(entity.getId())){
            entity.setId(IdUtils.fastSimpleUUID());
        }
                entity.setCreateBy(SecurityUtils.getUsername());
                entity.setCreateTime(DateUtils.getNowDate());
            return lvFapiaoMapper.insert(entity);
    }

    @Override
    public int insertFp(LvFapiao entity) {
        if(StringUtils.isBlank(entity.getId())){
            entity.setId(IdUtils.fastSimpleUUID());
        }
        entity.setCreateBy(SecurityUtils.getUsername());
        entity.setCreateTime(DateUtils.getNowDate());
        entity.setDdSongpeople(SecurityUtils.getUsername());
        lvFapiaoMapper.insert(entity);

        LvBuy lvBuy=lvBuyMapper.selectById(entity.getCaigouid());
        lvBuy.setKaipiao(lvBuy.getKaipiao().add(entity.getKaipiao()));
        lvBuy.setShengyukp(lvBuy.getMoney().subtract(lvBuy.getKaipiao()));
        List<GoodsSalebuyall> goodsSalebuyalls=goodsSalebuyallMapper.selectMainId(entity.getCaigouid());
        for(GoodsSalebuyall goodsSalebuyall:goodsSalebuyalls){
            if(StringUtils.isNotBlank(goodsSalebuyall.getFapiaoId())){
                goodsSalebuyall.setFapiaoId(goodsSalebuyall.getFapiaoId()+","+entity.getId());
            }else{
                goodsSalebuyall.setFapiaoId(entity.getId());
            }
            if(StringUtils.isNotBlank(goodsSalebuyall.getFapiaono())){
                goodsSalebuyall.setFapiaono(goodsSalebuyall.getFapiaono() + "," + entity.getNos());
            }else{
                goodsSalebuyall.setFapiaono(entity.getNos());
            }
            goodsSalebuyallMapper.update(goodsSalebuyall);
            List<GoodsSalebuy> goodsSalebuyList=goodsSalebuyMapper.selectByCgallId(goodsSalebuyall.getId());
            for(GoodsSalebuy goodsSalebuy:goodsSalebuyList){
                if(StringUtils.isNotBlank(goodsSalebuy.getFapiaoId())){
                    goodsSalebuy.setFapiaoId(goodsSalebuy.getFapiaoId()+","+entity.getId());
                }else{
                    goodsSalebuy.setFapiaoId(entity.getId());
                }
                if(StringUtils.isNotBlank(goodsSalebuy.getFapiaono())){
                    goodsSalebuy.setFapiaono(goodsSalebuy.getFapiaono() + "," + entity.getNos());
                }else{
                    goodsSalebuy.setFapiaono(entity.getNos());
                }
                goodsSalebuyMapper.update(goodsSalebuy);
            }
        }
        lvBuy.setFpState(BusinessConstants.TAX_STATUS_1);
        lvBuyMapper.update(lvBuy);
        if(lvBuy.getShengyukp().compareTo(BigDecimal.ZERO)==0){
            lvBuy.setFpState(BusinessConstants.TAX_STATUS_2);
            return lvBuyMapper.update(lvBuy);
        }else{
            return 1;
        }
    }

    /**
     * 修改到货发票
     * @param entity 到货发票
     * @return 结果
     */
    @Override
    public int update(LvFapiao entity) {
                entity.setUpdateBy(SecurityUtils.getUsername());
                entity.setUpdateTime(DateUtils.getNowDate());
        return lvFapiaoMapper.update(entity);
    }

    /**
     * 批量删除到货发票
     * @param ids 需要删除的到货发票主键
     * @return 结果
     */
    @Override
    public int deleteByIds(String[] ids) {
        return lvFapiaoMapper.deleteByIds(ids);
    }

    /**
     * 删除到货发票信息
     * @param id 到货发票主键
     * @return 结果
     */
    @Override
    public int deleteById(String id) {
        return lvFapiaoMapper.deleteById(id);
    }
}
